﻿namespace TestTool.GUI.Utils.Profiles
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using TestTool.Tests.Common.CommonUtils;
    using TestTool.Tests.Definitions.Data;
    using TestTool.Tests.Definitions.Enums;
    using TestTool.Tests.Definitions.Interfaces;
    using TestTool.Tests.Definitions.Trace;

    public class ProfilesSupportInfo
    {
        public static Dictionary<Functionality, bool> CheckTestResults(Dictionary<TestInfo, TestStatus> log)
        {
            new List<TestInfo>().AddRange(log.Keys);
            Dictionary<Functionality, bool> dictionary = new Dictionary<Functionality, bool>();
            foreach (TestInfo info in log.Keys)
            {
                bool flag = ((TestStatus) log[info]) == TestStatus.Passed;
                foreach (Functionality functionality in info.FunctionalityUnderTest)
                {
                    if (dictionary.ContainsKey(functionality))
                    {
                        if (!flag && dictionary[functionality])
                        {
                            dictionary[functionality] = false;
                        }
                    }
                    else
                    {
                        dictionary.Add(functionality, flag);
                    }
                }
            }
            using (Dictionary<Functionality, bool>.KeyCollection.Enumerator enumerator3 = dictionary.Keys.GetEnumerator())
            {
                while (enumerator3.MoveNext())
                {
                    Functionality current = enumerator3.Current;
                }
            }
            return dictionary;
        }

        public static ProfilesSupportInfo LoadPreliminary(IEnumerable<IProfileDefinition> profiles, IEnumerable<TestInfo> allTests, IEnumerable<TestInfo> selectedTests, IEnumerable<Feature> features)
        {
            List<Functionality> list = new List<Functionality>();
            List<Functionality> list2 = new List<Functionality>();
            Dictionary<IProfileDefinition, List<Functionality>> dictionary = new Dictionary<IProfileDefinition, List<Functionality>>();
            Dictionary<IProfileDefinition, List<Functionality>> dictionary2 = new Dictionary<IProfileDefinition, List<Functionality>>();
            foreach (TestInfo info in selectedTests)
            {
                foreach (Functionality functionality in info.FunctionalityUnderTest)
                {
                    if (!list.Contains(functionality))
                    {
                        list.Add(functionality);
                    }
                }
            }
            foreach (IProfileDefinition definition in profiles)
            {
                List<Functionality> list3 = new List<Functionality>();
                List<Functionality> list4 = new List<Functionality>();
                dictionary.Add(definition, list3);
                dictionary2.Add(definition, list4);
                foreach (FunctionalityItem item in definition.Functionalities)
                {
                    if ((item.Features != null) && (item.Features.Length > 0))
                    {
                        bool flag = false;
                        foreach (Feature feature in item.Features)
                        {
                            if (!features.ContainsFeature(feature))
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag)
                        {
                            if (!definition.IsFunctionalityOptional(item.Functionality))
                            {
                                list4.Add(item.Functionality);
                            }
                            else
                            {
                                list3.Add(item.Functionality);
                            }
                            continue;
                        }
                    }
                    if (!list.Contains(item.Functionality) && !list2.Contains(item.Functionality))
                    {
                        bool flag2 = false;
                        foreach (TestInfo info2 in allTests)
                        {
                            if ((info2.FunctionalityUnderTest != null) && info2.FunctionalityUnderTest.Contains(item.Functionality))
                            {
                                flag2 = true;
                                break;
                            }
                        }
                        if (!flag2)
                        {
                            list2.Add(item.Functionality);
                        }
                    }
                }
            }
            return new ProfilesSupportInfo { FunctionalityUnderTests = list, FunctionalityWithoutTests = list2, OptionalFunctionalityUnderSkippedTests = dictionary, MandatoryFunctionalityWithoutFeatures = dictionary2 };
        }

        public Dictionary<Functionality, bool> FunctionalitiesDefined { get; private set; }

        public List<Functionality> FunctionalityUnderTests { get; private set; }

        public List<Functionality> FunctionalityWithoutTests { get; private set; }

        public Dictionary<IProfileDefinition, List<Functionality>> MandatoryFunctionalityWithoutFeatures { get; private set; }

        public Dictionary<IProfileDefinition, List<Functionality>> OptionalFunctionalityUnderSkippedTests { get; private set; }
    }
}

